package org.csu.molediary.api.controller.back;

import org.csu.molediary.api.common.CommonResponse;
import org.csu.molediary.api.entity.Account;
import org.csu.molediary.api.service.AccountService;
import org.csu.molediary.api.service.AdminAccountService;
import org.csu.molediary.api.service.AdminSignOnService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/admin/")
public class AdminAccountController {

    @Autowired
    AdminAccountService adminAccountService;

    //管理员对用户的删除
    @PostMapping("deleteuser")
    @ResponseBody
    public CommonResponse<String> deleteUserById(@RequestParam String id){
        return adminAccountService.deleteAccountByAccountId(id);
    }

    //管理员对用户的更改
    @PostMapping("changeuserinformation")
    @ResponseBody
    public CommonResponse<Account> changeUserInformation(
            @RequestParam("id") String id,
            @RequestParam("sex") String sex,
            @RequestParam("name")String name,
            @RequestParam("sign")String sign,
            @RequestParam("location")String location
    ){

        //获取到原来的account信息
        CommonResponse<List<Account>> accountCommonResponse1=adminAccountService.selectAccountById(id);
        Account account = accountCommonResponse1.getData().get(0);
        CommonResponse<Account> accountCommonResponse = CommonResponse.createForAdminSuccess("",account);
        accountCommonResponse.getData().setSex(sex);
        accountCommonResponse.getData().setName(name);
        accountCommonResponse.getData().setSign(sign);
        accountCommonResponse.getData().setLocation(location);

        //返回修改后的信息
        return adminAccountService.updateAccountInformation(accountCommonResponse.getData());

    }

    //管理员对所有用户的查看
    @GetMapping("selectalluser")
    @ResponseBody
    public CommonResponse<List<Account>> selectAllUser(){
        return adminAccountService.getAllAccount();
    }

    //管理员对单个用户的查找
    @GetMapping("selectuser/{id}")
    @ResponseBody
    public CommonResponse<List<Account>> selectOneUser(@PathVariable("id")String id){

        return adminAccountService.selectAccountById(id);
    }
}
